package com.wdh.dbutil.ykzx;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class YkzxApp {

    public static void main(String[] args)throws Exception {
        YkzxApp app = new YkzxApp();
        app.createBaiduRsuMapping();
    }

    /**
     * 建立百度rsu和云控智行rsu的对应关系
     * */
    private void createBaiduRsuMapping()throws Exception{
        JedisCluster jedisCluster = getEdgeRedis();
        Map<String, String> rcuMapping = jedisCluster.hgetAll("ConvertRcuId");

        Map<String,String> baiduRsuMap = new HashMap<>();
        for (Map.Entry<String, String> entry : rcuMapping.entrySet()) {
            String baiduRsuId = entry.getKey();
            String ykRcuId = entry.getValue();
            String crossId = getRcuCrossId(ykRcuId);
            if( StringUtils.isEmpty(crossId) ){
                System.out.println("not found crossId,rcuId =  " + ykRcuId);
                continue;
            }
            String rsuId = getRsuFromCrossId(crossId);
            if( StringUtils.isEmpty(rsuId) ){
                System.out.println("not found rsuId,crossId = " + crossId);
                continue;
            }
            baiduRsuMap.put(baiduRsuId,rsuId);
        }

        jedisCluster.hmset("ConvertRsuId",baiduRsuMap);
        jedisCluster.close();

        JedisCluster rcRedis = getRcRedis();
        rcRedis.hmset("ConvertRsuId",baiduRsuMap);
        rcRedis.close();
    }

    /**
     * 根据rcuId查询rsuId
     * */
    private String getRcuCrossId(String rcuId)throws Exception{
        DataSource ds = getDataSource();
        String sql = String.format("select rs.cross_id from tb_device_info dev,tb_roadside_info rs WHERE dev.is_vaild = 0 and  dev.is_bind = 2 and dev.device_id = "
                                    + "'%s' and dev.bind_id = rs.rs_pt_id and rs.isvaild = 0 limit 1",rcuId);
        QueryRunner queryRunner = new QueryRunner(ds);
        Map<String, Object> rs = queryRunner.query(sql, new MapHandler());
        if( rs.size() > 0 ){
            return rs.get("cross_id").toString();
        }
        return "";
    }

    /**
     * 根据路口id查询rsu
     * */
    private String getRsuFromCrossId(String crossId)throws Exception{
        String sql = String.format(" select dev.device_id from tb_device_info dev,tb_roadside_info rs " +
                " where dev.is_vaild = 0 and dev.is_bind = 2 and dev.type = 'R' " +
                " and dev.bind_id = rs.rs_pt_id and rs.isvaild = 0 and rs.cross_id = '%s' limit 1",crossId);
        DataSource ds = getDataSource();
        QueryRunner queryRunner = new QueryRunner(ds);
        Map<String, Object> rs = queryRunner.query(sql, new MapHandler());
        if( rs.size() > 0 ){
            return rs.get("device_id").toString();
        }
        return "";
    }

    private JedisCluster getRcRedis() {
        Set<HostAndPort> nodes = new HashSet<>();
        String redisCfg = "10.16.1.145:6379,10.16.1.146:6380,10.16.1.147:6381,10.16.1.146:6379,10.16.1.147:6380,10.16.1.145:6381";
        String redisPass = "Ykzx@rcredis";
        String[] arr = redisCfg.split(",");
        for (int i = 0; i < arr.length; i++) {
            String[] item = arr[i].split(":");
            String host = item[0];
            String port = item[1];
            nodes.add(new HostAndPort(host, Integer.parseInt(port)));
        }
        JedisCluster jedisCluster = new JedisCluster(nodes, 2000, 2000, 5, redisPass, new GenericObjectPoolConfig());
        return jedisCluster;
    }

    private JedisCluster getEdgeRedis() {
        Set<HostAndPort> nodes = new HashSet<>();
        String redisCfg = "10.16.1.128:6379,10.16.1.129:6380,10.16.1.130:6381,10.16.1.128:6381,10.16.1.129:6379,10.16.1.130:6380";
        String redisPass = "Ykzx@edredis";
        String[] arr = redisCfg.split(",");
        for (int i = 0; i < arr.length; i++) {
            String[] item = arr[i].split(":");
            String host = item[0];
            String port = item[1];
            nodes.add(new HostAndPort(host, Integer.parseInt(port)));
        }
        JedisCluster jedisCluster = new JedisCluster(nodes, 2000, 2000, 5, redisPass, new GenericObjectPoolConfig());
        return jedisCluster;
    }

    private  DataSource getDataSource() throws PropertyVetoException {
        ComboPooledDataSource ds = new ComboPooledDataSource();
        ds.setDriverClass("com.mysql.cj.jdbc.Driver");
        ds.setJdbcUrl("jdbc:mysql://10.16.1.153:3306/base_info");
        ds.setUser("App");
        ds.setPassword("App@2352!");
        return ds;
    }
}
